class MatMul:
    def input_shape_correct(self, shapes):
        #print('input_shape_correct, MatMul')

        if len(shapes) != 2:
            return False

        if len(shapes[0]) == 1 and len(shapes[1]) == 1:
            shapes[0].append(shapes[1][0])
            shapes[1].append(1)
            return True

        if len(shapes[0]) > 2 and len(shapes[1]) > 2 and len(shapes[0]) < len(shapes[1]) :
            tmp = shapes[1]
            shapes[1] = shapes[0]
            shapes[0] = tmp

        if len(shapes[0]) > 2 and len(shapes[1]) > 2 and len(shapes[0]) == len(shapes[1]) :
            diff = len(shapes[0]) - len(shapes[1])
            s1 = shapes[0][0:-2]
            s2 = shapes[1][-2:]
            #print('ZZZZZZZZZZZZZZZ diff=', diff)
            shape = s1
            shape.extend(s2)
            shapes[1] = shape
            #for i in range(len(shapes[0]) - 2) :
            #    shapes[1][-3-i] = shapes[0][-3-i]

        if len(shapes[0]) > 2 and len(shapes[1]) > 2 and len(shapes[0]) > len(shapes[1]) :
            #print('MMMMMM++++++++++', len(shapes[0]), len(shapes[1]))
            for i in range(len(shapes[1]) - 2) :
                #print('MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM-------------', i)
                shapes[1][-3-i] = shapes[0][-3-i]    

        if len(shapes[0]) == 1 and len(shapes[1]) > 1:
            if shapes[0][0] != shapes[1][-2] :
                    #print('correct shapes 1-------', shapes[1][0], shapes[0][0])

                    shapes[0][0] = shapes[1][-2]

                    return True

        if len(shapes[0]) > 1 and len(shapes[1]) == 1:
            if shapes[0][-1] != shapes[1][0] :
                    #print('correct shapes 2-------', shapes[0][-1], shapes[1][0])

                    shapes[1][0] = shapes[0][-1]

                    return True

        if len(shapes[0]) >= 2 and len(shapes[1]) >= 2:
            if shapes[0][-1] != shapes[1][-2] :
                    #print('correct shapes 3-------', shapes[0][-1], shapes[1][-2] )

                    shapes[1][-2] = shapes[0][-1]

                    return True

    def infer_output_shape(self, shapes):
        shape = []

        if len(shapes) != 2:
            return shape, False

        if len(shapes[0]) >= 2 and len(shapes[1]) >= 2 :
            shape = shapes[0]
            shape[-1] = shapes[1][-1]
            return shape, True

        if len(shapes[0]) == 1 and len(shapes[1]) == 1 :
            return shape, True
        
        if len(shapes[0]) == 1  :
            for i in range(len(shapes[1]) - 2):
                shape.append(shapes[1][i])

            shape.append(shapes[1][-1])    

            return shape, True

        if len(shapes[1]) == 1  :
            for i in range(len(shapes[0]) - 1):
                shape.append(shapes[0][i])

            return shape, True    
           